#import "FlutterNativeLocationPlugin.h"
#import <CoreLocation/CoreLocation.h>
#import <AMapFoundationKit/AMapFoundationKit.h>
#import <AMapLocationKit/AMapLocationKit.h>

@interface FlutterNativeLocationPlugin ()<AMapLocationManagerDelegate>

@property (nonatomic, strong) AMapLocationManager *locationManager;
@end
//通道名称
static NSString *const CHANNEL_NAME = @"com.ehs2.flutter_albs_map";
//通道方法
static NSString *const CHANNEL_METHOD = @"getCurrentLocation";
// 高德地图 key
static NSString *const GDMAP_KEY = @"76ffe13cd5b5e54de8a438aaec4fc8ca";

@implementation FlutterNativeLocationPlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
  FlutterMethodChannel* channel = [FlutterMethodChannel
      methodChannelWithName:CHANNEL_NAME
            binaryMessenger:[registrar messenger]];
  FlutterNativeLocationPlugin* instance = [[FlutterNativeLocationPlugin alloc] init];
    [AMapLocationManager updatePrivacyShow:AMapPrivacyShowStatusDidShow privacyInfo:AMapPrivacyInfoStatusDidContain];
    [AMapLocationManager updatePrivacyAgree:AMapPrivacyAgreeStatusDidAgree];
  [registrar addMethodCallDelegate:instance channel:channel];
}

- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
  if ([CHANNEL_METHOD isEqualToString:call.method]) {
      [self kInitLocationInfo:call result:result];
  } else {
    result(FlutterMethodNotImplemented);
  }
}
/**
 *初始化定位
 */
- (void)kInitLocationInfo:(FlutterMethodCall*)call result:(FlutterResult)result{
    
//    NSString *apiKey = call.arguments;
//    if ([apiKey isEqualToString:@"apiKey"] || apiKey.length < 0) {
//
//      return  NSLog(@"apiKey不能为空");
//    }
    
    [AMapServices sharedServices].apiKey = GDMAP_KEY;//call.arguments;
    self.locationManager = [[AMapLocationManager alloc] init];
    // 带逆地理信息的一次定位（返回坐标和地址信息）
    [self.locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
    //   定位超时时间，最低2s，此处设置为10s
    self.locationManager.locationTimeout = 10;
    //   逆地理请求超时时间，最低2s，此处设置为10s
    self.locationManager.reGeocodeTimeout = 10;
    
    self.locationManager.delegate = self;
    
    
    [self.locationManager requestLocationWithReGeocode:YES completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
        NSDictionary *dic = [self getLacationDicWithLocation:location regeocode:regeocode error:error];
        result(dic);
    }];
    
      
}
/**
*高德地图
*解决 ios 11 无法定位
*/
- (void)amapLocationManager:(AMapLocationManager *)manager doRequireLocationAuth:(CLLocationManager*)locationManager
{
    [locationManager requestAlwaysAuthorization];
}

- (NSDictionary *)getLacationDicWithLocation:(CLLocation *)location regeocode:(AMapLocationReGeocode *)regeocode error:(NSError *)error{
    
    NSMutableDictionary *locationInfoDic  = [NSMutableDictionary dictionary];
    locationInfoDic[@"latitude"] = [NSNumber numberWithDouble:location.coordinate.latitude];;
    locationInfoDic[@"longitude"] = [NSNumber numberWithDouble:location.coordinate.longitude];
    locationInfoDic[@"country"] = regeocode.country;
    locationInfoDic[@"province"] = regeocode.province;
    locationInfoDic[@"cityCode"] = regeocode.citycode;
    locationInfoDic[@"city"] = regeocode.city;
    locationInfoDic[@"district"] = regeocode.district;
    locationInfoDic[@"street"] = regeocode.street;
    locationInfoDic[@"locationDescribe"] = regeocode.formattedAddress;
    locationInfoDic[@"adCode"] = regeocode.adcode;
    locationInfoDic[@"errorCode"] = [NSNumber numberWithInt:error.code];
    
    return locationInfoDic;
}

@end

